<template>
  <div class="login-container">
    <el-form
      ref="form"
      :model="form"
      label-width="80px"
      @keyup.enter.native="onSubmit"
      class="demo-ruleForm login-page"
    >
      <h3 class="title">系统登录</h3>

      <el-form-item label="用户名">
        <el-input
          v-model="form.user_name"
          placeholder="请填写您的用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input
          v-model="form.pass_word"
          show-password
          placeholder="请填写您的密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        user_name: "",
        pass_word: "",
      },
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      let that = this;
      this.$http.get("/get/user").then((res) => {
        if (res.data.state) {
          that.$store.commit("put_user", res.data.data);
          that.$router.push("/").catch(() => {});
        }
      });
    },
    onSubmit() {
      let that = this;
      this.$http.post("/login", this.form).then((res) => {
        if (res.data.state) {
          that.$message.success("登录成功");
          that.$store.commit("put_user", res.data.data);
          that.getUser();
          that.$router.push("/").catch(() => {});
        } else {
          that.$message.error(res.data.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
}
.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
label.el-checkbox.rememberme {
  margin: 0px 0px 15px;
  text-align: left;
}
.title {
  text-align: center;
}
</style>
